package com.ahao.userprivacy;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;

public class AhaoPrivacyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 移除标题
        requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 设置黑色背景
        getWindow().setBackgroundDrawableResource(android.R.color.black);
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Bundle metData = null;
        try {
            metData = getApplicationContext().getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA).metaData;
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        String activityName = metData.getString("ahao_privacy_activity_name");
        SharedPreferences sharedPreferences = getSharedPreferences("ahao_user_privacy", MODE_PRIVATE);
        boolean isAgree = sharedPreferences.getBoolean("isAgree", false);
        if (isAgree) {
            goMainActivity(activityName);
            return;
        }

        AhaoPrivacyDialog dialog = new AhaoPrivacyDialog(this);
        dialog.setOnAgreeListener(new AhaoPrivacyDialog.onAgreeListener() {
            @Override
            public void onAgree() {
                dialog.dismiss();
                sharedPreferences.edit().putBoolean("isAgree", true).apply();
                goMainActivity(activityName);
            }

            @Override
            public void onDisagree() {
                finish();
                System.exit(0);
            }
        }).show();
    }

    private void goMainActivity(String activityName) {
        Intent intent = new Intent();
        intent.setClassName(AhaoPrivacyActivity.this, activityName);
        startActivity(intent);
        finish();
    }
}
